package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chixing.entity.Product;
import com.chixing.mapper.ProductMapper;
import com.chixing.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-17
 */
@Service
public class ProductServiceImpl  implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Result getById(Integer productId) {
        Product product = productMapper.selectById(productId);
        if (product!=null)
            return Result.getSuccess(product);
        return Result.getFail(null);
    }

    @Override
    public Result getPage(Integer pageNumber) {
        Page<Product> page = new Page<>(pageNumber,3);
        page= productMapper.selectPage(page,null);
        if (page.getTotal()>0)
            return Result.getSuccess(page);
        return Result.getFail(null);
    }

    @Override
    public Result save(Product product) {
        int rows = productMapper.insert(product);
        if (rows>0)
            return Result.saveSuccess(product);
        return Result.saveFail(null);
    }

    @Override
    public Result update(Product product) {
        int rows = productMapper.updateById(product);
        if (rows>0)
            return Result.updateSuccess(product);
        return Result.updateFail(null);
    }

    @Override
    public Result delete(Integer productId) {
        int rows = productMapper.deleteById(productId);
        if (rows>0)
            return Result.DeleteSuccess(productId);
        return Result.DeleteFail(null);
    }

    @Override
    public Result getPro(String proName, Float proMinPrice, Float proMaxPrice, String proType) {
        List<Product> products = productMapper.selectConditions(proName, proMinPrice, proMaxPrice, proType);
        if (products!=null&&products.size()>0)
            return Result.getSuccess(products);
        return Result.getFail(null);
    }


}
